﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork5
{
    public class Order : IComparable
    {
        public int OrderID { get; set; }
        public Client Client { get; set; }
        public double TotalMoney { get; set; }
        public List<OrderItem>orderItems= new List<OrderItem>();
        public List<OrderItem> OrderItems { get => orderItems; set { orderItems = value; } }
        public Order(int OrderID, Client Client)
        {
            this.OrderID = OrderID;
            this.Client = Client;
            TotalMoney = 0;
        }
        public Order() { }
        public int CompareTo(Object obj)
        {
            Order order = obj as Order;
            if (order == null)
                throw new System.ArgumentException();
            return this.OrderID.CompareTo(order.OrderID);
        }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   OrderID == order.OrderID;
        }
        //添加订单明细
        public bool AddOrderItem(OrderItem orderItemNew)
        {
            bool repeated = false;
            foreach (OrderItem orderItem in this.OrderItems)
            {
                if (orderItem.Equals(orderItemNew))
                    repeated = true;
            }
            if (!repeated)
            {
                this.OrderItems.Add(orderItemNew);
                this.TotalMoney += orderItemNew.ProductNumber * orderItemNew.Product.ProductPrice;
                Console.WriteLine("添加成功！");
            }
            else { Console.WriteLine("已有重复订单明细！"); }
            return !repeated;
        }
        public override int GetHashCode()
        {
            var hashCode = 997595479;
            hashCode = hashCode * -1521134295 + OrderID.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Client.ClientName);
            return hashCode;
        }
        public override string ToString()
        {
            string str = $"OrderID: {OrderID} Client: {Client.ToString()} TotalMoney:{TotalMoney}\r\n";
            OrderItems.ForEach(orderItem => str += orderItem.ToString()+"\r\n");
            return str;
        }
    }
}
